using System;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class SwitchLanguage : MonoBehaviour
{
	public UISprite sprFlag;

	public UILabel lbLang;

	public GameObject popupLang;

	public List<CaseLang> listCaseLang = new List<CaseLang>();

	public List<GameObject> langauges = new List<GameObject>();

	public GameObject parentObject;

	public static event Action onChangeLanguage;

	private void Awake()
	{
		popupLang.SetActive(false);
		listCaseLang.AddRange(GetComponentsInChildren<CaseLang>(true));
		foreach (CaseLang item in listCaseLang)
		{
			item.swLang = this;
		}
		SetCurrentLang();
	}

	public void SetCurrentLang()
	{
		foreach (CaseLang item in listCaseLang)
		{
			if (item.key == LocalizationManager.CurrentLanguage)
			{
				SetLang(item);
				break;
			}
		}
	}

	public void SetLang(CaseLang curLang)
	{
		sprFlag.spriteName = curLang.sprImg.spriteName;
		lbLang.text = curLang.lbLang.text;
		popupLang.SetActive(false);
		OnValueChange(curLang.key);
		foreach (GameObject langauge in langauges)
		{
			langauge.GetComponent<CaseLang>().RefreshColor();
		}
	}

	public void OnValueChange(string keyLang)
	{
		if (LocalizationManager.CurrentLanguage != keyLang)
		{
			LocalizationManager.CurrentLanguage = keyLang;
			if (ManagerMissions.instance != null)
			{
				ManagerMissions.instance.LocalizeMissionText();
			}
			if (SwitchLanguage.onChangeLanguage != null)
			{
				SwitchLanguage.onChangeLanguage();
			}
		}
	}

	private void OnClick()
	{
		popupLang.SetActive(true);
	}
}
